﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;
using Serilog;

namespace TMI85303.UI.UserControls
{


    public class DragCmdSlider : Slider
    {
        public DragCmdSlider()
        {
            valueBeforeDrag = Value;
            //valueBeforeKeyDown = Value;
        }

        private double valueBeforeDrag;
        protected override void OnThumbDragStarted(DragStartedEventArgs e)
        {
            valueBeforeDrag = Value;
            base.OnThumbDragStarted(e);
        }

        protected override void OnThumbDragCompleted(DragCompletedEventArgs e)
        {
            base.OnThumbDragCompleted(e);
            if (valueBeforeDrag != Value)
            {
                Scroll?.Execute(ScrollParameter);
            }
        }

        protected override void OnMouseWheel(MouseWheelEventArgs e)
        {
            Value += e.Delta / 120;
            Scroll?.Execute(ScrollParameter);
            base.OnMouseWheel(e);
        }

        protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
        {

            // 获取Slider的Track部件
            var track = (Track)this.Template.FindName("PART_Track", this);
            if (track == null) 
            {
                base.OnPreviewMouseLeftButtonDown(e);
                return;
            }
            
            // 获取鼠标点击位置
            var position = e.GetPosition(track);

            // 检查点击位置是否在Thumb内
            var thumbRect = new Rect(track.Thumb.TranslatePoint(new Point(0, 0), track), new Size(track.Thumb.ActualWidth, track.Thumb.ActualHeight));
            if (!thumbRect.Contains(position))
            {
                // 如果点击位置不在Thumb内，则忽略此次点击，不对Slider的值进行更改
                e.Handled = true;
                return;
            }

            base.OnPreviewMouseLeftButtonDown(e);
        }

        //private double valueBeforeKeyDown;
        //protected override void OnKeyDown(KeyEventArgs e)
        //{
        //    base.OnKeyDown(e);

        //}

        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            Scroll?.Execute(ScrollParameter);

        }

        public ICommand Scroll
        {
            get { return (ICommand)GetValue(ScrollProperty); }
            set { SetValue(ScrollProperty, value); }
        }

        // Command DependencyProperty
        public static readonly DependencyProperty ScrollProperty =
            DependencyProperty.Register("Scroll", typeof(ICommand), typeof(DragCmdSlider), new PropertyMetadata(null));

        // CommandParameter DependencyProperty
        public object ScrollParameter
        {
            get { return GetValue(ScrollParameterProperty); }
            set { SetValue(ScrollParameterProperty, value); }
        }

        public static readonly DependencyProperty ScrollParameterProperty =
            DependencyProperty.Register("ScrollParameter", typeof(object), typeof(DragCmdSlider), new PropertyMetadata(null));

    }
}
